<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: Shuangfu
  Date: 2019-11-29
  Time: 14:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {
            var grade = '${stu.grade.gid}';
            if (grade == '') {
                grade = -1;
            }
            $("[name='grade.gid']").val(grade);

            //拿到当前页
            var currPage =${pager.currPage};
            //拿到最后一页
            var totalPages =${pager.totalPages};

            if (currPage <= 1) {
                $("#prev").removeAttr("href");
            }
            if (currPage >= totalPages) {
                $("#next").removeAttr("href");
            }

        });

        function toPage(page) {
            $("#myForm").attr("action", "select?currPage=" + page);
            $("#myForm").submit();
        }

        function aa() {
            $("[name='grade.gid']").val(-1);
            $("[name='sname']").val("");
        }

        function del(sno) {
            if (confirm("确定要删除吗？")) {
                location.href = "delete/" + sno;
            }
        }
    </script>
</head>
<body>
<c:if test="${param.op=='delYes'}">
    <script>
        alert("删除成功");
    </script>
</c:if>
<c:if test="${param.op=='delNo'}">
    <script>
        alert("删除失败");
    </script>
</c:if>
<c:if test="${param.op=='insertYes'}">
    <script>
        alert("新增成功");
    </script>
</c:if>
<c:if test="${param.op=='delNo'}">
    <script>
        alert("删除失败");
    </script>
</c:if>
<form id="myForm" action="./select" method="post">
    <center>
        <select name="grade.gid">
            <option value="-1">请选择</option>
            <c:forEach items="${gradeAll}" var="grade">
                <option value="${grade.gid}">${grade.gname}</option>
            </c:forEach>
        </select>
        <input type="text" name="sname" value="${stu.sname}"/>
        <input type="submit" value="查询"/>
        <input type="button" onclick="aa()" value="重置"/>
        <br/>
        <a href="toInsert">新增</a>
    </center>
    <table align="center" width="70%" border="1px">
        <tr align="center">
            <td>学生编号</td>
            <td>学生姓名</td>
            <td>出生日期</td>
            <td>学生年级</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${list}" var="user" varStatus="st">
            <tr align="center"
                    <c:if test="${st.index%2==0}">
                        style="background-color: gray"
                    </c:if>
            >
                <td>${user.sno}</td>
                <td>${user.sname}</td>
                <td><fmt:formatDate value="${user.birthdate}" pattern="yyyy-MM-dd"/></td>
                <td>${user.grade.gname}</td>
                <td>
                    <a href="javaScript:del('${user.sno}')">删除</a>
                    <a href="toUpdate/${user.sno}">修改</a>
                </td>
            </tr>
        </c:forEach>
    </table>

    <center>
        <a href="javaScript:toPage(1)">首页</a>&nbsp;&nbsp;
        <a id="prev" href="javaScript:toPage(${pager.currPage-1})">上一页</a>
        <a id="next" href="javaScript:toPage(${pager.currPage+1})">下一页</a>
        <a href="javaScript:toPage(${pager.totalPages})">尾页</a>&nbsp;&nbsp;
        <span>第${pager.currPage}页/共${pager.totalPages}页</span>
    </center>

</form>
</body>
</html>
